/**
  ******************************************************************************
  * @file    EEPROM_Emulation/inc/eeprom.h
  * @author  MCD Application Team
  * @version V3.1.0
  * @date    07/27/2009
  * @brief   This file contains all the functions prototypes for the EEPROM
  *          emulation firmware library.
  ******************************************************************************
  * @copy
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2009 STMicroelectronics</center></h2>
  ******************************************************************************
  * @author .ISS_AUTO (hudandan@issauto.com)
  * @version V3.1.1
  * @date 2024-02-23  MCU-N32G45x 移植记录:
  *                   1. 新增函数 InitCurrWrAddress();
  *                   2. 重定义函数 FLASH_ErasePage() & FLASH_ProgramHalfWord()
  *                      (注，N32G45x的Flash编程只能进行字编程);
  *                   3. 修复页状态切换BUG: 原ST-Demo中，页状态 由RECEIVE_DATA（0xEEEE）
  *                      重新编程为VALID_PAGE（0x0000）的操作过程，就相当于不擦除恢复到全“1”，
  *                      而是直接将其他可编程的“1”写成“0”。
  *                      !!! 但该方法并不适用于N32G45x这颗芯片，必须先擦除再写入，所以这里本人
  *                      采用2个编程单位大小的空间来是实现3个页状态的标记切换;
  *
  * @copyright Copyright (c) 2024 ISSAUTO TECH Co., Ltd. All rights reserved.
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __EEPROM_H
#define __EEPROM_H

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Exported constants --------------------------------------------------------*/
/* Define the N32G455CCL7(Flash= 256KB) Flash page size */
#define PAGE_SIZE  (uint16_t)0x800  /* Page size = 2KByte */

/* EEPROM start address in Flash */
/* EEPROM emulation start address: after 250KByte of used Flash memory */
// #define EEPROM_START_ADDRESS    ((uint32_t)0x0803E800)
#define EEPROM_START_ADDRESS    (FLASH_ADDR_USER_START)

#define EEPROM_PAGE_STS_SIZE    ((uint32_t)0x00000008) //!< 页状态标识大小

/* Pages 0 and 1 base and end addresses */
#define PAGE0_BASE_ADDRESS      ((uint32_t)(EEPROM_START_ADDRESS + 0x0000))
#define PAGE0_END_ADDRESS       ((uint32_t)(EEPROM_START_ADDRESS + (PAGE_SIZE - 1)))

#define PAGE1_BASE_ADDRESS      ((uint32_t)(EEPROM_START_ADDRESS + PAGE_SIZE))
#define PAGE1_END_ADDRESS       ((uint32_t)(EEPROM_START_ADDRESS + (2 * PAGE_SIZE - 1)))

/* Used Flash pages for EEPROM emulation */
#define PAGE0                   ((uint16_t)0x0000)
#define PAGE1                   ((uint16_t)0x0001)

/* No valid page define */
#define NO_VALID_PAGE           ((uint16_t)0x00AB)

/* Page status definitions */
#define ERASED                  ((uint32_t)0xFFFFFFFF)     /* PAGE is empty 页为空 */
#define RECEIVE_DATA            ((uint32_t)0xEEEEEEEE)     /* PAGE is marked to receive data 页正在从另一个满页接收数据 */
#define VALID_PAGE              ((uint32_t)0x00000000)     /* PAGE containing valid data 页中包含有效数据，并且在将所有有效数据完全传输到已擦除页之前此状态不会改变 */

/* Valid pages in read and write defines */
#define READ_FROM_VALID_PAGE    ((uint8_t)0x00)
#define WRITE_IN_VALID_PAGE     ((uint8_t)0x01)

/* Page full define */
#define PAGE_FULL               ((uint8_t)0x80)

/* Variables' number */
#define EE_VIRT_ADD_VAR_MASK    ((uint16_t)0xDD00)
// #define NumbOfVar               ((uint8_t)0x03)

/* Exported types ------------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
uint16_t E2PROM_Init(const uint16_t *pVirtAddTab, uint16_t pVirtAddTabSize);
uint16_t EE_ReadVariable(uint16_t VarIdx, uint16_t *Data, uint8_t WholePage);
uint16_t EE_WriteVariable(uint16_t VarIdx, uint16_t Data);

uint16_t EE_Init(void);
// uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data);
// uint16_t EE_WriteVariable(uint16_t VirtAddress, uint16_t Data);

#endif /* __EEPROM_H */

/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
